﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Hotnet.Biz.NSBizPicture;
using Hotnet.Entity;
using Hotnet.Web.Utility;
using Hotnet.Biz.NSBizCommon;
using Hotnet.Entity.Enum;
using Hotnet.Entity.Search;

namespace Hotnet.Web.Controllers.Admin
{
	public class AdminPictureCategoryController : AdminBaseController
	{
		//
		// GET: /AdminPictureCategroy/
		private BizPictureCategory biz = new BizPictureCategory();
		private BizPictureGroup bizGroup = new BizPictureGroup();
		public static SelectList PictureNetList
		{
			get
			{
				BizSourceFrom s = new BizSourceFrom();
				List<SourceFrom> list = s.List(EnumSystemCategory.图片).ToList();
				SourceFrom temp = new SourceFrom();
				temp.SourceFromID = -1;
				temp.Name = "全部";
				list.Insert(0, temp);
				return new SelectList(list, "SourceFromID", "Name");
			}
		}
		public static SelectList PictureCategroyList
		{
			get
			{
				BizPictureCategory p = new BizPictureCategory();
				List<PictureCategory> list = p.List().ToList();
				PictureCategory temp = new PictureCategory();
				temp.CategoryID = -1;
				temp.Name = "全部";
				list.Insert(0, temp);
				return new SelectList(list, "CategoryID", "Name");
			}
		}

		private BizPictureList bizPictureList;
		private BizPictureList BizPictureList
		{
			get
			{
				if (bizPictureList == null)
					bizPictureList = new BizPictureList();
				return bizPictureList;
			}
		}

		private BizSourceFrom bizSourceFrom;
		private BizSourceFrom BizSourceFrom
		{
			get
			{
				if (bizSourceFrom == null)
				{
					bizSourceFrom = new BizSourceFrom();
				}
				return bizSourceFrom;
			}
		}
		public ActionResult List()
		{
			var categoryList = biz.List(x => x.OrderNo, false);
			return View(categoryList);
		}

		public ActionResult Index(int id, int? pageNo,SearchPictureCondition searchObj)
		{
			int totalCount;
			//var groupList = bizGroup.List(id, pageNo ?? 0, PAGE_SIZE, out totalCount);
			searchObj.CategoryID = id;
			PictureGroup[] groupList = bizGroup.List(searchObj, pageNo ?? 0, PAGE_SIZE, out totalCount,searchObj.IsPublished);
			ViewData["PaginateHelper"] = PaginateHelper.ConstructPaginate(totalCount, pageNo ?? 0, PAGE_SIZE, "/AdminPictureCategory/Index/" + id, searchObj);
			ViewData["Category"] = biz.Get(id);
			SourceFrom[] sourceFrom = BizSourceFrom.List(EnumSystemCategory.图片);
			SelectList sourceFromList = new SelectList(sourceFrom, "SourceFromID", "Name", null);
			ViewData["SourceFromList"] = sourceFromList;
			return View(groupList);
		}

		public ActionResult ListPictureList(int? pageNo,SearchPictureList searchObj)
		{
			int totalCount;
			if (searchObj.IsPublished == EnumPublishState.全部)
				searchObj.IsPublished = null;
			if (searchObj.PictureCategroyId == -1)
				searchObj.PictureCategroyId = null;
			ViewData["PictureList"]=BizPictureList.List(searchObj,pageNo??0,PAGE_SIZE,out totalCount);
			ViewData["PaginateHelper"] = PaginateHelper.ConstructPaginate(totalCount, pageNo ?? 0, PAGE_SIZE, "/AdminPictureCategory/ListPictureList", searchObj);
			return View();
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public ActionResult SetPictureListPublished(int id,EnumPictureListType listType,int publishedState)
		{
			PictureList pl = BizPictureList.Get(id, listType);
			pl.IsPublished = publishedState;
			BizPictureList.Update(x => x.GroupID == id && x.ListType == (int)listType, y => new PictureList() { IsPublished = publishedState });
			return Content(Convert.ToString((int)EnumOperateState.成功));
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public ActionResult DeletePictureList(int id, EnumPictureListType listType)
		{
			BizPictureList.Delete(id, listType);
			return Content(Convert.ToString((int)EnumOperateState.成功));
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(PictureCategory category)
		{
			if (category.OrderNo == null)
				category.OrderNo = 0;
			biz.Insert(category);
			return RedirectToAction("List");
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(PictureCategory pictureCategory)
		{
			biz.Update(pictureCategory);
			return RedirectToAction("List");
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(int id)
		{
			biz.Delete(id);
			return RedirectToAction("List");
		}
	}
}